<?php
/**购物车管理*/
namespace App\Http\Controllers;

use App\Http\Models\ShopCar;
use App\Json\Json;
use Illuminate\Support\Facades\Input;
use Symfony\Component\HttpFoundation\Request;

class ShopCarController extends Controller
{
    /**
     * 购物车列表
     * @id 用户id
    */
    public function shopcar_list()
    {
        $param = [
            'id' => Input::get('id')
        ];
        $data = ShopCar::shopcar_list($param);
        $result = ['code'=>200,'messages'=>'家具分类','result'=>$data];
        return response()->json($result);
    }

    /**
     * 购物车编辑
     * @param [type=>1 新增，2 修改编辑]
    */
    public function shopcar_edit()
    {
        $type = trim(Input::get('type'));
        if($type==1){ //添加购物车操作
            $request = array(
                'cart_fitment_id' => array('value'=>Input::get('cart_fitment_id'),'isnumber'=>true,'empty'=>true,'declare'=>'租赁家具详情id'),
                'cart_user_id' => array('value'=>Input::get('cart_user_id'),'isnumber'=>true,'empty'=>true,'declare'=>'用户id'),
                'cart_fitment_num' => array('value'=>Input::get('cart_fitment_num'),'isnumber'=>true,'empty'=>true,'declare'=>'租赁数量'),
                'cart_rent_type' => array('value'=>Input::get('cart_rent_type'),'isnumber'=>true,'empty'=>true,'declare'=>'租赁方式'),
                'cart_use_time' => array('value'=>Input::get('cart_use_time'),'isnumber'=>false,'empty'=>false,'declare'=>'家具使用时间')
            );
            $param= array();
            foreach ($request as $key=>$val) {
                if($val['empty']){
                    if(empty($val['value'])){
                        $result = ['code'=>'1004','messages'=>$key.$val['declare'].'为必填参数'];
                        return response()->json($result);
                    }
                    if($val['isnumber'] && !is_numeric($val['value'])){
                        $result = ['code'=>'1002','messages'=>$key.$val['declare'].'参数类型必须是数字'];
                        return response()->json($result);
                    }
                }
                $param[$key]=$val['value'];
            }
            $data = ShopCar::shopcar_add($param);
            $result = ['code'=>200,'messages'=>'新增购物车操作','result'=>$data];
            return response()->json($result);
        } else if($type==2){ //删除购物车操作
            $request = array(
                'id' => array('value'=>Input::get('id'),'isnumber'=>true,'empty'=>true,'declare'=>'购物车id'),
               // 'cart_user_id' => array('value'=>Input::get('cart_user_id'),'isnumber'=>true,'empty'=>true,'declare'=>'用户id'),
                'cart_fitment_id' => array('value'=>Input::get('cart_fitment_id'),'isnumber'=>true,'empty'=>true,'declare'=>'租赁家具详情id'),
                'cart_rent_type' => array('value'=>Input::get('cart_rent_type'),'isnumber'=>true,'empty'=>true,'declare'=>'租赁方式'),
                'cart_fitment_num' => array('value'=>Input::get('cart_fitment_num'),'isnumber'=>false,'empty'=>true,'declare'=>'租赁数量')
            );
            $param= array();
            foreach ($request as $key=>$val) {
                if($val['empty']){
                    if(empty($val['value'])){
                        $result = ['code'=>'1004','messages'=>$key.$val['declare'].'为必填参数'];
                        return response()->json($result);
                    }
                    if($val['isnumber'] && !is_numeric($val['value'])){
                        $result = ['code'=>'1002','messages'=>$key.$val['declare'].'参数类型必须是数字'];
                        return response()->json($result);
                    }
                }
                $param[$key]=$val['value'];
            }
            $data = ShopCar::shopcar_edit($param);
            $result = ['code'=>200,'messages'=>'购物车编辑操作','result'=>$data];
            return response()->json($result);
        }else{
            $result = ['code'=>1002,'messages'=>'看不懂你的操作,type类型值有误'];
            return response()->json($result);
        }
    }

    /**购物车删除*/
    public function shopcar_destroy(Request $request)
    {
        $param=[
            'cart_fitment_id'=>Input::get('cart_fitment_id',$request)
        ];
       $f_id = explode(',',$param['cart_fitment_id']);
        if(count($f_id)<=1){
           $id = $f_id[0];
        }else{
            $id = $f_id;
        }
        $data = ShopCar::shopcart_destroy($id);
        if($data){
            $result = ['code'=>200,'msg'=>'成功'];
        }else{
            $result = ['code'=>400,'msg'=>'失败'];
        }
        return Json::json($result['code'],$result['msg'],$id);
    }

    /**
     * 购物车结算【生成未付款订单】
     */
    public function shopcar_finish()
    {
        //
    }
}
